/*
 *
 *      Copyright (c) 2018-2025, st All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 *  this list of conditions and the following disclaimer.
 *  Redistributions in binary form must reproduce the above copyright
 *  notice, this list of conditions and the following disclaimer in the
 *  documentation and/or other materials provided with the distribution.
 *  Neither the name of the pig4cloud.com developer nor the names of its
 *  contributors may be used to endorse or promote products derived from
 *  this software without specific prior written permission.
 *  Author: st
 *
 */

package com.mkh.st.admin.api.util;

import cn.hutool.core.bean.BeanUtil;
import com.mkh.st.admin.api.dto.MenuDTO;
import com.mkh.st.admin.api.dto.MenuTree;
import com.mkh.st.admin.api.dto.TreeNode;
import com.mkh.st.admin.api.entity.SysMenu;
import com.mkh.st.admin.api.vo.RoleMenuTree;
import com.mkh.st.admin.api.vo.RoleMenuTreeVO;
import com.mkh.st.admin.api.vo.RoleTree;
import com.mkh.st.admin.api.vo.RoleTreeInfoVO;
import lombok.experimental.UtilityClass;

import java.util.ArrayList;
import java.util.List;

/**
 * @author st
 * @date 2020-02-09
 */
@UtilityClass
public class TreeUtil {

	/**
	 * 两层循环实现建树
	 * @param treeNodes 传入的树节点列表
	 * @return
	 */
	public <T extends TreeNode> List<T> build(List<T> treeNodes, Object root) {

		List<T> trees = new ArrayList<>();

		for (T treeNode : treeNodes) {

			if (root.equals(treeNode.getParentId())) {
				trees.add(treeNode);
			}

			for (T it : treeNodes) {
				if (it.getParentId().equals(treeNode.getId()) ) {
					if (treeNode.getChildren() == null) {
						treeNode.setChildren(new ArrayList<>());
					}
					treeNode.add(it);
				}
			}
		}
		return trees;
	}

	/**
	 * 使用递归方法建树
	 * @param treeNodes
	 * @return
	 */
	public <T extends TreeNode> List<T> buildByRecursive(List<T> treeNodes, Object root) {
		List<T> trees = new ArrayList<T>();
		for (T treeNode : treeNodes) {
			if (root.equals(treeNode.getParentId())) {
				trees.add(findChildren(treeNode, treeNodes));
			}
		}
		return trees;
	}

	/**
	 * 递归查找子节点
	 * @param treeNodes
	 * @return
	 */
	public <T extends TreeNode> T findChildren(T treeNode, List<T> treeNodes) {
		for (T it : treeNodes) {
			if (treeNode.getId().equals(it.getParentId()) ) {
				if (treeNode.getChildren() == null) {
					treeNode.setChildren(new ArrayList<>());
				}
				treeNode.add(findChildren(it, treeNodes));
			}
		}
		return treeNode;
	}

	/**
	 * 通过sysMenu创建树形节点
	 * @param menus
	 * @param root
	 * @return
	 */
	public List<MenuTree> buildTree(List<SysMenu> menus, int root) {
		List<MenuTree> trees = new ArrayList<>();
		MenuTree node;
		for (SysMenu menu : menus) {
			node = new MenuTree();
			node.setId(menu.getMenuId());
			node.setParentId(menu.getParentId());
			node.setName(menu.getName());
			node.setPath(menu.getPath());
			node.setPermission(menu.getPermission());
			node.setLabel(menu.getName());
			node.setIcon(menu.getIcon());
			node.setType(menu.getType());
			node.setSort(menu.getSort());
			node.setHasChildren(true);
//			node.setParentName(menu.g());
			trees.add(node);
		}
		return TreeUtil.build(trees, root);
	}

	/**
	 * 通过sysMenu创建树形节点
	 * @param menus
	 * @param root
	 * @return
	 */
	public List<MenuTree> buildTreeDto(List<MenuDTO> menus, Long root) {
		List<MenuTree> trees = new ArrayList<>();
		for (MenuDTO menu : menus) {
			MenuTree menuTree= BeanUtil.toBean(menu,MenuTree.class);
			menuTree.setId(menu.getMenuId());
			menuTree.setHasChildren(true);
			trees.add(menuTree);
		}
		return TreeUtil.build(trees, root);
	}

	/**
	 * 角色树
	 * @param roles
	 * @param root
	 * @return
	 */
	public List<RoleTree> buildRoleTree(List<RoleTreeInfoVO> roles, Long root) {
		List<RoleTree> trees = new ArrayList<>();
		for (RoleTreeInfoVO item : roles) {
			RoleTree roleTree= BeanUtil.toBean(item,RoleTree.class);
			roleTree.setId(item.getRoleId());
			roleTree.setHasChildren(true);
			trees.add(roleTree);
		}
		return TreeUtil.build(trees, root);
	}

	public List<RoleMenuTree> buildRoleMenuTree(List<RoleMenuTreeVO> roles, Long root) {
		List<RoleMenuTree> trees = new ArrayList<>();
		for (RoleMenuTreeVO item : roles) {
			RoleMenuTree roleMenuTree= BeanUtil.toBean(item,RoleMenuTree.class);
			roleMenuTree.setId(item.getMenuId());
			roleMenuTree.setHasChildren(true);
			trees.add(roleMenuTree);
		}
		return TreeUtil.build(trees, root);
	}
}
